**Lightsoft standard PPC macros (c) 1995/6.
**bil (branch immedaite long) is used for routines that have been defined as a 
**toc entry using the 'toc_routine' directive. Useful for >16 bit branches
bil:	macro
	ifc	"","\1"	*needs a label,else we fail
	 fail
	endc
	lwz	r10,[T]\1(rtoc)	*its a toc call
	mtctr	r10
	bctr
	endm

**bill is same as bil, but puts return address in link register so callee can get back via
**link.
bill:	macro
	ifc	"","\1"	*needs a label else we fail
	 fail
	endc
	lwz	r10,[T]\1(rtoc)	*its a toc call
	mtctr	r10
	bctrl
	endm
***************************************************************
**Clr - clears a register
clr:	macro
	ifne	NARG-1		*Needs 1 argument
	 fail
	endif
	
	sub	\1,\1,\1
	endm	
****************************************************************
**inc - increments a register by 1
inc:	macro
	ifne	NARG-1		*Needs 1 argument
	 fail
	endif
	addic	\1,\1,1
	endm

****************************************************************
**dec - decrements a register by 1
dec:	macro
	ifne	NARG-1		*Needs 1 argument
	 fail
	endif
	subic	\1,\1,1
	endm

************
**Xcall import_name
**eg Xcall SysBeep
**Glue code to system functions
**Can be assumed to destroy lr,ctr,d3-12
Xcall:	macro	
*set up params
	lwz	r12,\1(rtoc)	*load t-vector
	stw	R2,20(sp)	*save my RTOC
	lwz	r0,0(r12) 	*get callee address
	lwz	R2,4(r12)	*set callee RTOC
	mtctr	r0		*prepare branch
	bctrl			*bsr to callee

	lwz	r2,20(sp)	*get my toc back
	endm

********start up code
**Saves the gpr's and sets up the stack and BSS pointer - r30
start_up:	macro
	mflr	r0				* Get link register
	stw	r0,8(sp)			* Store the link resgister on the stack
**save r10-31
	stw	r10,-88(sp)
	stw	r11,-84(sp)
	stw	r12,-80(sp)
	stw	r13,-76(sp)
	stw	r14,-72(sp)
	stw	r15,-68(sp)
	stw	r16,-64(sp)
	stw	r17,-60(sp)
	stw	r18,-56(sp)
	stw	r19,-52(sp)
	stw	r20,-48(sp)
	stw	r21,-44(sp)
	stw	r22,-40(sp)
	stw	r23,-36(sp)
	stw	r24,-32(sp)
	stw	r25,-28(sp)
	stw	r26,-24(sp)
	stw	r27,-20(sp)
	stw	r28,-16(sp)
	stw	r29,-12(sp)
	stw	r30,-8(sp)
	stw	r31,-4(sp)
	stwu	sp,-64+88(sp)		* skip over the stack space where the caller

	lwz	r30,(RTOC)	*load global data (bss) pointer - first entry in TOC

	endm

*****************************
**tidy_up restores the gp regs and restores the stack
tidy_up:	macro

	lwz	r0,64+8+88(sp)	* Get the saved link register
	mtlr	r0			* put saved link register in link register
	addi	sp,sp,$40+88	* Reset the stack pointer
**restore r10-31
	lwz	r10,-88(sp)
	lwz	r11,-84(sp)
	lwz	r12,-80(sp)
	lwz	r13,-76(sp)
	lwz	r14,-72(sp)
	lwz	r15,-68(sp)
	lwz	r16,-64(sp)
	lwz	r17,-60(sp)
	lwz	r18,-56(sp)
	lwz	r19,-52(sp)
	lwz	r20,-48(sp)
	lwz	r21,-44(sp)
	lwz	r22,-40(sp)
	lwz	r23,-36(sp)
	lwz	r24,-32(sp)
	lwz	r25,-28(sp)
	lwz	r26,-24(sp)
	lwz	r27,-20(sp)
	lwz	r28,-16(sp)
	lwz	r29,-12(sp)
	lwz	r30,-8(sp)
	lwz	r31,-4(sp)
	endm

